#include "p2p.h"
#define BUFFSIZE  4096

static char buf[BUFFSIZE];
static send_cmd(char *cmd, int sockfd)
{
	if(send(sockfd, cmd, strlen(cmd), 0) == -1)
	{return -1;}
	return 0;
}


static int recvResponse(int sockfd)
{
	int retval, nread;
	fd_set readfd;
	struct timeval timeout;
	char res_code[4];

	FD_ZERO(&readfd);
	FD_SET(sockfd, &readfd);
	timeout.tv_sec = 10;
	timeout.tv_usec = 0;
	retval = select(sockfd + 1, &readfd, NULL, NULL, &timeout);
	if(retval == -1){return -1;}
	else if(retval == 0){
		perror("receive response from server time out!\n");
		return -1;
	}else {
		nread = recv(sockfd, buf, BUFFSIZE, 0);
		if((nread == -1) ||(nread == 0))
		{return -1;}
		buf[nread-1] = '\0';
		buf[nread-2] = '\0';
	}
	strncpy(res_code, buf, 3);
	res_code[3] = '\0';
	return atoi(res_code);
		
}



int getReqSize(char *pp[], const int count)
{
	int size;
	int counts, retval;
	int sockfd;
	struct sockaddr_in sa;
	char *ip;
	int n;
	struct addrinfo *ai;
	ai = host_serv(pp[0], NULL, 0, 0);
	ip = Sock_ntop_host(ai->ai_addr, ai->ai_addrlen);

	printf("connect to the server %s.....\n", ip);
	//pp[0] : ftp.cup.edu.cn
	//pp[count] : 11414_14_d_1.wmv

	
	memset(&sa, 0, sizeof(sa));
	sa.sin_family = AF_INET;
	sa.sin_port = htons(21);
	sa.sin_addr.s_addr = inet_addr(ip);
	
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	for(counts = 0; counts < 5; counts++)
	{
		retval = connect(sockfd, (struct sockaddr *)&sa, sizeof(sa));
		if(retval == 0)
			break;
		if(retval == -1){
			if(errno == 115){continue;}
			else{return -1;}
		}
	}
	if(counts < 5)
	{
		if(recvResponse(sockfd) == 220)
		{printf("%s\n", buf);}
		else{printf("the server no repons\n");
			return -1;
		}
	}
	char name[] = "anonymous";
	char passwd[] = "123456";
	char name_buf[32];
	char pass_buf[32];
	int num;

	sprintf(name_buf, "USER %s\r\n", name);
	send_cmd(name_buf, sockfd);
	if((num = recvResponse(sockfd)) == -1)
	{
		perror("user error");
		exit(1);
	}else if(num != 331)
	{
		exit(1);
	}

	sprintf(pass_buf, "PASS %s\r\n", passwd);
	send_cmd(pass_buf, sockfd);

	if((num = recvResponse(sockfd)) == -1)
	{
		perror("pass error");
		exit(1);
	}
	else if(num != 230)
	{
		exit(1);
	}

			
	for(n = 1; n < count; n++)
	{
		char cmd_line[64];
		sprintf(cmd_line, "CWD %s\r\n", pp[n]);
		send_cmd(cmd_line, sockfd);
		if((num = recvResponse(sockfd)) != 250)
		{
			perror("cd error");
			exit(1);
		} 
	}
	
	char cmd_buf[64];
	sprintf(cmd_buf, "SIZE %s\r\n", pp[count]);
	send_cmd(cmd_buf, sockfd);
	if(recvResponse(sockfd) != 213)
	{
		perror("size error");
		exit(1);
	}
	char sizecmd[1024];
	strcpy(sizecmd, buf+4);
	printf("%s\n", sizecmd);
	size = atoi(sizecmd);
	close(sockfd);
	return size;

}

/*int main(int argc, char **argv)
{
	int i, reqlen, size;
	char *pp[10], *p;
	int count_pp;
	char urlBuff[1024];
	printf("please input your url request:\n");
	scanf("%s", urlBuff);

	reqlen = strlen(urlBuff);
	urlBuff[reqlen] = '\0';

	i = 0;
	pp[0] = urlBuff;
	while(p)
	{
		
		p = strchr(pp[i], '/');
		if(p == NULL)
			break;
		
		*p = '\0';
		i++;
		pp[i] = (++p);
	}
	count_pp = i;
	//=================================================================
	printf("the len of url %d \n", reqlen);
	for(i = 0; i <= count_pp; i++)
		{
			printf("req  %s\n", pp[i]);
		}
	//=================================================================


	size = getReqSize(pp,count_pp); 
	printf("the size was :%d\n", size);
	return 0;
}
*/
